﻿package net.jmp0.toast.entities.effects
{
	import punk.Textplus;
	import net.jmp0.toast.entities.player.Player;
	
	/**
	 * ...
	 * @author Thodd
	 */
	public class GameText extends Textplus
	{
		
		private var shakeX:int = 0;
		private var shakeY:int = 0;
		
		private var queue:Array = new Array();
		
		private var shakeDelay:int = 0;
		
		private var shaking:Boolean = true;
		
		
		public function GameText(text:String, x:int, y:int, font:String, size:int, depth:int, shaking:Boolean) {
			super(text, x, y);
			this.font = font;
			this.size = size;
			this.depth = depth;
			this.shaking= shaking;
			this.active = true;
			
			if (shaking) {
				initialiseShakeQueue();
			}
			
			FP.world.add(this);
		}
		
		override public function update():void {
			
			if(shaking){
				shakeDelay += 1;
			
				if(shakeDelay >= 8){
					var e:int = queue.shift();
			
					switch(e) {
						case 0: shakeX = 0; shakeY = 0; break;
						case 1: shakeX = 1; shakeY = 0; break;
						case 2: shakeX = 0; shakeY = -1; break;
						case 3: shakeX = -1; shakeY = 0; break;
						case 4: shakeX = 0; shakeY = 1; break;
					}
			
					queue.push(e);
				
					shakeDelay = 1;
					
					x += shakeX;
					y += shakeY;
				}
		
			}
			
		}
		
		public function setShaking(b:Boolean):void {
			shaking = b;
			if (queue.length == 0) {
				initialiseShakeQueue();
			}
		}
		
		private function initialiseShakeQueue():void {
			for (var i:int = 0; i <= 4; i++) {
				queue.push(i);
			}
			var rnd:int = Math.round(4 * Math.random());
			for (i = 0; i <= rnd; i++) {
				var e:int = queue.shift();
				queue.push(e);
			}
		}
		
		public function destroy():void {
			trace("gametext destroyed");
			FP.world.remove(this);
		}
		
		public function getShakeX():int {
			return shakeX;
		}
		
		public function getShakeY():int {
			return shakeY;
		}
		
	}

}